using System;
using Unity.Profiling;
using UnityEngine;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace WorldMapBase.Tile.Pool
{
    public class RtsPrefabPool : ObjectPool<GameObject>
    {
        private ResLoader _resLoader;
        private int _initNum;
        private string _resPath;
        private Transform _root;
        protected GameObject _originObject;
        public string Name = "";
        public int priority;
        public bool loaded;
        
        private static ProfilerMarker preloadMarker = new ProfilerMarker("RtsPrefabPool.PreloadRes");
        private static ProfilerMarker instantiateMarker = new ProfilerMarker("RtsPrefabPool.Instantiate");
        
        public RtsPrefabPool(int initNum,int capacity,string resPath,Transform root) : 
            base(capacity,true,1,5,10)
        {
#if UNITY_EDITOR
            var strs = resPath.Split('/');
            _name = "RtsPrefabPool: " + strs[strs.Length - 1];
#else
            var strs = resPath.Split('/');
            Name = strs[strs.Length - 1];
#endif

            _initNum = initNum;
            _resPath = resPath;
            _root = root;
        }

        
        public void PreLoad(Action<GameObject> completeAction,bool isDynamicAdd = false)
        {
            if (_resLoader == null)
            {
                _resLoader = ResLoader.Alloc();
            }
            //preloadMarker.Begin();
            _resLoader.Add2Load(_resPath, typeof(GameObject), ((success, name, asset) =>
            {
                //preloadMarker.End();
                if (asset == null)
                {
                    Debug.LogError("实例化失败："+_resPath);
                }
                else
                {
                    instantiateMarker.Begin();
                    GameObject tmp = GameObject.Instantiate(asset as GameObject, GlobalValue.INVISIBLE_POSITION, Quaternion.identity,_root);
                    _originObject = tmp;
                    var initNUm = isDynamicAdd ? 1 : _initNum;
                    base.OnInitAsync(initNUm);
                    completeAction?.Invoke(tmp);
                    instantiateMarker.End();
                }
                
            }));
            _resLoader.Load();
        }
        

        protected override GameObject ConstructItem()
        {
            if (_originObject == null)
            {
                Debug.LogEditorError("RtsPrefabPool：资源还未加载完成" + _resPath);
                return null;
            }
            
            GameObject obj = 
                GameObject.Instantiate(_originObject, GlobalValue.INVISIBLE_POSITION, Quaternion.identity,_root);
            return obj;
        }
        
        protected override void BeforeRecover(GameObject obj)
        {
            obj.transform.position = GlobalValue.INVISIBLE_POSITION;
        }

        protected override void OnRemoveItem(GameObject trans)
        {
            GameObject.Destroy(trans);
        }
        
        public override void Destroy()
        {
            base.Destroy();
            if (_resLoader != null)
            {
                _resLoader.Recycle2Cache();
                _resLoader = null;
                _originObject = null;
            }
        }
        
    }
}
